package conf

import (
	"fmt"
	"gitee.com/alex-pistachio/axel/utils"
	"github.com/jinzhu/configor"
	"os"
	"path/filepath"
)

func InitConfig(cfg interface{}) error {
	profile := os.Getenv("PROFILE")
	configFile := "config.toml"
	if profile != "" {
		configFile = fmt.Sprintf("config-%s.toml", profile)
	}

	//先在可执行文件路径下搜索配置文件，搜索不到，改到当前路径继续搜索
	cfgPath := filepath.Join(utils.CurrentDir(), configFile)
	if utils.IsFileExists(cfgPath) {
		err := configor.Load(cfg, cfgPath)
		if err != nil {
			return err
		}
	} else {
		cfgPath := "config.toml"
		err := configor.Load(cfg, cfgPath)
		if err != nil {
			return err
		}
	}

	return nil
}

/* Config struct example
type SysConfig struct {
	Mysql struct {
		DataSource      string `toml:"DataSource"`
		ConnMaxLifetime int    `toml:"ConnMaxLifetime"  default:"1800"`
		MaxIdleConns    int    `toml:"MaxIdleConns" default:"5"`
		MaxOpenConns    int    `toml:"MaxOpenConns" default:"10"`
	} `toml:"Mysql"`

	Server struct {
		Port string `toml:"Port" default:":8080"`
	} `toml:"Server"`

	Log struct {
		LogType    string `toml:"LogType" default:"console"` //#console or file
		Filename   string `toml:"Filename"`
		MaxBackups int    `toml:"MaxBackups"`
		MaxSize    int    `toml:"MaxSize"`
		MaxAge     int    `toml:"MaxAge"`
	} `toml:"Log"`

	Account struct {
		ContractAddress string `toml:"ContractAddress" required:"true" `
	} `toml:"Account" `

	DID struct {
		Method    string `toml:"method" required:"true" `
		Chaincode string `toml:"chaincode" required:"true"`
	}
}
*/
